package main.java.cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiredPermission(name="数据字典页面",expression="systemDictionary:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "systemDictionary/list";
    }


    @RequiredPermission(name="数据字典删除",expression="systemDictionary:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list";
    }


    @RequiredPermission(name="数据字典新增/编辑",expression="systemDictionary:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary) {
        if (systemDictionary.getId() == null) {
            systemDictionaryService.save(systemDictionary);
        } else {
            systemDictionaryService.update(systemDictionary);
        }
        return "redirect:/systemDictionary/list";
    }


    @RequiredPermission(name="数据字典新增/编辑",expression="systemDictionary:input")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("systemDictionary",systemDictionaryService.get(id));
        }
        return "systemDictionary/input";
    }
}
